package com.authserver.mapper;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Set;

@Repository
public class UserMapper {

    private final static List<UserDetails> APPLICATION_USERS;

    static {
        Set<SimpleGrantedAuthority> role = Collections.singleton(new SimpleGrantedAuthority("ROLE_USER"));
        User user = new User("123@qq.com", "123456", role);
        APPLICATION_USERS = Collections.singletonList(user);
    }

    public UserDetails findUserByEmail(String email) {
        return APPLICATION_USERS
                .stream()
                .filter(u -> u.getUsername().equals(email))
                .findFirst()
                .orElseThrow(() -> new UsernameNotFoundException("No user was found"));
    }
}
